import {
    getMyInformation,
    getAllSoldArtworkFake,
    getMyFocus,
    getOwnerArray,
    getMyNft,
    getMySelling,
    getMyShopByAudit,
    getMyColletion,
    getMySold,
    getParticipateAuction
} from "@/api"

const state = {
    userInfo: {},
    soldArtworks: [],
    focusList: {},
    myCollections: {},
    myCreated: {},
    mySelling: {},
    myNotOnSell: {},
    mySole: {},
    myLikes: {},
    myParticipateAuction: {}
}

const mutations = {
    USERINFO(context, data) {
        context.userInfo = data
    },
    SOLDARTWORKS(context, data) {
        context.soldArtworks = data
    },
    FOCUSLIST(context, data) {
        context.focusList = data
    },
    MYCOLLECTIONS(context, data) {
        context.myCollections = data
    },
    MYCREATED(context, data) {
        context.myCreated = data
    },
    MYSELLING(context, data) {
        context.mySelling = data
    },
    MYNOTONSELL(context, data) {
        context.myNotOnSell = data
    },
    MYSOLE(context, data) {
        context.mySole = data
    },
    MYLIKES(context, data) {
        context.myLikes = data
    },
    MYPARTICIPATEAUCTION(context, data) {
        context.myParticipateAuction = data
    },
    CLEARMYNOTONSELL(context) {
        context.myNotOnSell = {}
    },


}

const actions = {
    async userInfo({
        commit
    }, address) {
        let result = await getMyInformation()
        commit("USERINFO", result[0])
    },
    async soldArtworks({
        commit
    }, address) {
        let result = await getAllSoldArtworkFake()
        commit("SOLDARTWORKS", result)
    },
    async focusList({
        commit
    }, params) {
        let {
            pageIndex,
            pageSize
        } = params
        let result = await getMyFocus(pageIndex, pageSize)
        commit('FOCUSLIST', result)
    },
    async myCollections({
        commit
    }, params) {
        let {
            pageIndex,
            pageSize
        } = params
        let result = await getOwnerArray(pageIndex, pageSize)
        commit('MYCOLLECTIONS', result)
    },
    async myCreated({
        commit
    }, params) {
        let {
            pageIndex,
            pageSize
        } = params
        let result = await getMyNft(pageIndex, pageSize)
        commit('MYCREATED', result)
    },
    async mySelling({
        commit
    }, params) {
        let {
            pageIndex,
            pageSize
        } = params
        let result = await getMySelling(pageIndex, pageSize)
        commit('MYSELLING', result)
    },
    async myNotOnSell({
        commit
    }, params) {
        commit('CLEARMYNOTONSELL')
        let result = await getMyShopByAudit(params)
        commit('MYNOTONSELL', result)
    },
    async mySole({
        commit
    }, params) {
        let result = await getMySold(params)
        commit("MYSOLE", result)
    },
    async myLikes({
        commit
    }, params) {
        let result = await getMyColletion(params)
        commit('MYLIKES', result)
    },
    async myParticipateAuction({
        commit
    }, address) {
        let result = await getParticipateAuction(address)
        commit('MYPARTICIPATEAUCTION', result)
    },
    userLogin({
        commit
    }, userInfo) {
        commit("USERINFO", userInfo)
    }
}

const getters = {
    user_address(state) {
        return state.userInfo.user_address
    },
    // 过滤出正在拍卖的商品
    myBids(state) {
        if (state.mySelling.data == null) {
            return
        }
        let data = state.mySelling.data.filter(item => {
            return item.form_of_sale == '拍卖'
        })
        return data
    }
}

export default {
    state,
    mutations,
    actions,
    getters
}